home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / database / postgres / mpsql-1.001 / mpsql-1~ / mpsql-1.0 / llist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-28  |  6.5 KB  |  178 lines

  1. /************************************************************************/
  2. /* File   : llist.c                                        */
  3. /* Purpose: linked list  module                                         */
  4. /* By     : Keith R. Davis                                */
  5. /* Date   : 12/27/95                                        */
  6. /* Notes  : Copyright(c) 1996 White River Software                */
  7. /************************************************************************/
  8.  
  9. #include <Xm/Xm.h>            /* motif lib header        */
  10. #include <Xm/MessageB.h>        /* message box header        */
  11. #include <Xm/Text.h>                  /* text widget header        */
  12. #include <string.h>            /* string header        */
  13. #include <stdio.h>                      /* stdio header                 */
  14.  
  15. #include "mquel.h"                      /* mquel header                 */
  16. #include "llist.h"                      /* linked list header           */
  17. #include "db.h"                         /* db header                    */
  18.  
  19. LLISTbuffer *buffer_head;   /* buffer list head                     */
  20. LLISTbuffer *buffer_tail;   /* buffer list tail                     */
  21. LLISTbuffer *buffer_curr;   /* current pos in buffer list           */
  22.  
  23. /************************************************************************/
  24. /* Function: LLIST_Init                                                 */
  25. /* Purpose : inits the linked list of buffers                           */
  26. /* Params  :                                                            */
  27. /* Returns : ptr to list tail  on SUCCESS / NULL on FAILURE             */
  28. /* Notes   :                                                            */
  29. /************************************************************************/
  30.  
  31. LLISTbuffer* LLIST_Init(void)
  32. {
  33.   if((buffer_head = (LLISTbuffer*)XtMalloc(sizeof(LLISTbuffer))) == NULL)
  34.      return NULL;
  35.  
  36.   if((buffer_tail = (LLISTbuffer*)XtMalloc(sizeof(LLISTbuffer))) == NULL){
  37.     XtFree((char*)buffer_head); 
  38.     return NULL;
  39.   }
  40.  
  41.   buffer_head->buffer = XmCreateText(AppWidgetsPtr->shell, NULL, NULL, 0);
  42.   buffer_head->next = buffer_tail;
  43.   buffer_head->prev = buffer_head;
  44.   buffer_tail->next = buffer_tail;
  45.   buffer_tail->prev = buffer_head;
  46.  
  47.   buffer_curr = buffer_tail;
  48. }
  49.  
  50. /************************************************************************/
  51. /* Function: LLIST_Delete                                               */
  52. /* Purpose : deletes a buffer node from the linked list                 */
  53. /* Params  : node : ptr to node to delete                               */
  54. /* Returns :                                                            */
  55. /* Notes   :                                                            */
  56. /************************************************************************/
  57.  
  58. void LLIST_Delete(LLISTbuffer *node)
  59. {
  60.   if(((node == buffer_head) && (node->next != buffer_tail)) ||
  61.      ((node == buffer_tail) && (node->prev != buffer_head))){
  62.     return;
  63.   }
  64.   else if(((node == buffer_head) && (node->next == buffer_tail)) ||
  65.       ((node == buffer_tail) && (node->prev == buffer_head))){
  66.     return;
  67.   }
  68.   else {
  69.     if(buffer_curr == node)
  70.       buffer_curr = node->prev;
  71.     node->next->prev = node->prev;
  72.     node->prev->next = node->next;
  73.     XtDestroyWidget(node->buffer);
  74.     XtFree(node->filename);
  75.     XtFree((char*)node);
  76.   }
  77. }
  78.  
  79. /************************************************************************/
  80. /* Function: LLIST_Insert                                               */
  81. /* Purpose : inserts a buffer node into the linked list                 */
  82. /* Params  : mod : modified flag 0=FALSE / 1=TRUE                       */
  83. /*           file: buffer's associated filename                         */ 
  84. /*           node : ptr to node to insert before                        */
  85. /* Returns : ptr to inserted node                                       */
  86. /* Notes   :                                                            */
  87. /************************************************************************/
  88.  
  89. LLISTbuffer* LLIST_Insert(int mod, char *file, LLISTbuffer *node)
  90. {
  91.   LLISTbuffer *insert;
  92.  
  93.   if((insert = (LLISTbuffer*)XtMalloc(sizeof(LLISTbuffer))) == NULL)
  94.     return NULL;
  95.  
  96.   if((insert->filename = (char*)XtMalloc(sizeof(char)*MAX_PATH_LEN)) == NULL){
  97.     XtFree((char*)insert);
  98.     return NULL;
  99.   }
  100.  
  101.   insert->buffer = XmCreateText(AppWidgetsPtr->shell, NULL, NULL, 0);
  102.   
  103.   insert->modified = mod;
  104.   strcpy(insert->filename, file);
  105.   insert->next = node;
  106.   insert->prev = node->prev;
  107.   node->prev->next = insert;
  108.   node->prev = insert;
  109.   return insert;
  110. }
  111.  
  112. /************************************************************************/
  113. /* Function: LLIST_Remove                                               */
  114. /* Purpose : destroys the linked list                                   */
  115. /* Params  :                                                            */
  116. /* Returns :                                                            */
  117. /* Notes   :                                                            */
  118. /************************************************************************/
  119.  
  120. void LLIST_Remove(void)
  121. {
  122.   while(buffer_head->next != buffer_tail)
  123.     LLIST_Delete(buffer_head->next);
  124.   
  125.   buffer_curr = NULL;
  126.  
  127.   XtFree((char*)buffer_head);
  128.   XtFree((char*)buffer_tail); 
  129. }
  130.  
  131. /************************************************************************/
  132. /* Function: LLIST_Count                                                */
  133. /* Purpose : returns the count of nodes in the linked list              */
  134. /* Params  :                                                            */
  135. /* Returns : count of nodes in the list                                 */
  136. /* Notes   :                                                            */
  137. /************************************************************************/
  138.  
  139. int LLIST_Count(void)
  140. {
  141.   int count = 0;
  142.   LLISTbuffer *trav_ptr = buffer_head->next;
  143.  
  144.   while(trav_ptr != buffer_tail){
  145.     count++;
  146.     trav_ptr = trav_ptr->next;
  147.   }
  148.   return count;
  149. }
  150.  
  151. /************************************************************************/
  152. /* Function: LLIST_Get                                                  */
  153. /* Purpose : returns a list node by index                               */
  154. /* Params  : index : position of node in list rel to the head           */
  155. /* Returns : ptr to the node                                            */
  156. /* Notes   :                                                            */
  157. /************************************************************************/
  158.  
  159. LLISTbuffer* LLIST_Get(int index)
  160. {
  161.   int count = LLIST_Count();
  162.   int i = 1;
  163.   LLISTbuffer *node = buffer_head->next;
  164.  
  165.   if(index > count)
  166.     return NULL;
  167.  
  168.   while(i < index){
  169.     i++;
  170.     node = node->next;
  171.   }
  172.   return node;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.